{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "initial_id",
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    ""
   ]
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-29T03:12:30.813308Z",
     "start_time": "2025-09-29T03:12:28.805287Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\n",
    "# 1、获取大模型\n",
    "import os\n",
    "import dotenv\n",
    "from langchain_core.messages import AIMessage\n",
    "from langchain_openai import ChatOpenAI\n",
    "\n",
    "dotenv.load_dotenv()\n",
    "\n",
    "os.environ['OPENAI_API_KEY'] = os.getenv(\"OPENAI_API_KEY1\")\n",
    "os.environ['OPENAI_BASE_URL'] = os.getenv(\"OPENAI_BASE_URL\")\n",
    "\n",
    "# 创建大模型实例\n",
    "llm = ChatOpenAI(model=\"gpt-4o-mini\")"
   ],
   "id": "daea21d78e869b9c",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/opt/miniconda3/envs/pyth310/lib/python3.10/site-packages/requests/__init__.py:86: RequestsDependencyWarning: Unable to find acceptable character detection dependency (chardet or charset_normalizer).\n",
      "  warnings.warn(\n"
     ]
    }
   ],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-29T03:13:15.910823Z",
     "start_time": "2025-09-29T03:12:34.360165Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\n",
    "from langchain_core.messages import HumanMessage\n",
    "from langchain_core.prompts import ChatPromptTemplate\n",
    "from langchain_core.messages.ai import AIMessage\n",
    "\n",
    "\n",
    "def chat_with_model(answer):\n",
    "    # 2、提供提示词模板：ChatPromptTemplate\n",
    "    prompt_template = ChatPromptTemplate.from_messages([\n",
    "        (\"system\", \"你是一个人工智能的助手\"),\n",
    "        (\"human\", \"{question}\")\n",
    "    ])\n",
    "\n",
    "    while True:\n",
    "        # 3、获取chain，并调用大模型得到响应\n",
    "        chain = prompt_template | llm\n",
    "        response = chain.invoke({\"question\": answer})\n",
    "\n",
    "        # 4、输出大模型的响应\n",
    "        print(f\"模型回复：{response.content}\")\n",
    "\n",
    "        # 5、继续获取用户的问题\n",
    "        user_input = input(\"你还有其他问题吗？(输入'退出'时，结束会话)\")\n",
    "\n",
    "        # 6、指明退出循环的方式\n",
    "        if (user_input == \"退出\"):\n",
    "            break\n",
    "\n",
    "        # 7、将上述新生成的消息存放到提示词模板的消息列表中\n",
    "        prompt_template.messages.append(AIMessage(content=response.content))\n",
    "        prompt_template.messages.append(HumanMessage(content=user_input))\n",
    "\n",
    "\n",
    "chat_with_model(\"你好，很高兴认识你！\")"
   ],
   "id": "331de4d10539174b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "模型回复：你好！我也很高兴认识你！有什么我可以帮助你的吗？\n",
      "模型回复：投资美股可以通过以下几个步骤进行：\n",
      "\n",
      "1. **了解基础知识**：学习一些基本的股市知识，包括什么是股票、股市如何运作、如何分析公司财务报表等。\n",
      "\n",
      "2. **选择合适的券商**：在国内或海外选择一个可靠的证券公司进行开户。许多券商提供网上开户服务。一些知名的美国券商包括嘉信理财（Charles Schwab）、富途证券、TD Ameritrade 和 E*TRADE。\n",
      "\n",
      "3. **开户**：根据券商的要求提交相关文件进行身份验证。一般需要提供身份证明、地址证明以及税务信息等。\n",
      "\n",
      "4. **资金转入**：将资金转入你的交易账户。这可以通过银行转账或其他支付方式完成。\n",
      "\n",
      "5. **选择投资的股票**：根据自己的投资策略和市场研究选择要购买的股票。可以通过基本面分析、技术分析或其他方法进行评估。\n",
      "\n",
      "6. **下单交易**：在券商的平台上输入买入或卖出的指令。可以选择市价单、限价单等不同类型的订单。\n",
      "\n",
      "7. **监控投资**：定期查看你的投资组合，关注市场动态和公司新闻，必要时做出调整。\n",
      "\n",
      "8. **了解税务问题**：投资美股可能涉及到资本利得税和其他税务问题，最好了解相关法律法规，或咨询专业人士。\n",
      "\n",
      "9. **持续学习**：保持学习，关注市场变化和投资趋势，以便不断优化你的投资策略。\n",
      "\n",
      "记得投资有风险，尤其是在股市波动较大的情况下，务必根据自己的风险承受能力做出决策。希望这些信息对你有所帮助！如果还有其他问题，欢迎随时问我。\n",
      "模型回复：推荐具体股票时需要考虑多个因素，包括市场情况、企业财务状况、行业趋势等。虽然我无法提供个性化的投资建议，但我可以列出一些在市场上受到关注的行业或公司，供你参考：\n",
      "\n",
      "1. **科技公司**：\n",
      "   - **苹果公司（AAPL）**：以其强劲的品牌和创新产品闻名。\n",
      "   - **微软（MSFT）**：在云计算和软件服务方面具有较强的市场地位。\n",
      "   - **谷歌（GOOGL）**：拥有强大的广告业务和多元化的技术投资。\n",
      "\n",
      "2. **电动车和新能源**：\n",
      "   - **特斯拉（TSLA）**：电动车市场的领军企业。\n",
      "   - **宁德时代（Contemporary Amperex Technology Co., Ltd.，Trade on US Markets as CATL）**：在电池生产方面处于领先地位的公司。\n",
      "\n",
      "3. **消费品公司**：\n",
      "   - **可口可乐（KO）**：全球知名的饮料公司，稳定的现金流。\n",
      "   - **宝洁（PG）**：在日常消费品行业拥有多个知名品牌。\n",
      "\n",
      "4. **生物科技与医疗**：\n",
      "   - **辉瑞（PFE）**：制药公司，在疫苗和新药研发方面有重要成就。\n",
      "   - **赛诺菲（SNY）**：全球领先的制药公司，关注于生物医药。\n",
      "\n",
      "5. **金融行业**：\n",
      "   - **摩根大通（JPM）**：美国最大的银行之一，提供全方位金融服务。\n",
      "   - **美银（BAC）**：全球大型银行，经营多样化。\n",
      "\n",
      "在进行投资之前，建议你进行深入的调研和分析，了解每家公司最新的财务表现、市场动态和行业前景。同时，考虑自己的投资目标和风险承受能力。如果你不确定，可以考虑咨询专业的财务顾问。希望这些信息能够帮助你！\n"
     ]
    }
   ],
   "execution_count": 2
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
